//package com.majun.config.filter4;
//
//import cn.hutool.core.util.StrUtil;
//import org.springframework.http.server.ServletServerHttpRequest;
//import org.springframework.web.filter.CommonsRequestLoggingFilter;
//
//import javax.servlet.http.HttpServletRequest;
//
//public class MyRequestLoggingFilter extends CommonsRequestLoggingFilter {
//    @Override
//    protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
//        StringBuilder msg = new StringBuilder();
//        msg.append(prefix);
//        msg.append(StrUtil.format("method=[{}];", request.getMethod().toLowerCase()));
//        msg.append(StrUtil.format("url=[{}];", request.getRequestURL().toString()));
////        msg.append("uri=").append(request.getRequestURI());
//
//        if (isIncludeQueryString()) {
//            String queryString = request.getQueryString();
//            if (queryString != null) {
//                msg.append('?').append(queryString);
//            }
//        }
//
//        if (isIncludeClientInfo()) {
//            final String requestIp = getRequestIp(request);
//            msg.append(StrUtil.format("ip=[{}];", requestIp));
////            String client = request.getRemoteAddr();
////            if (StringUtils.hasLength(client)) {
////                msg.append(";client=").append(client);
////            }
////            HttpSession session = request.getSession(false);
////            if (session != null) {
////                msg.append(";session=").append(session.getId());
////            }
////            String user = request.getRemoteUser();
////            if (user != null) {
////                msg.append(";user=").append(user);
////            }
//        }
//
//        if (isIncludeHeaders()) {
//            msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
//        }
//
//        if (isIncludePayload()) {
//            String payload = getMessagePayload(request);
//            if (payload != null) {
//                msg.append(";payload=").append(payload);
//            }
//        }
//
//        msg.append(suffix);
//        return msg.toString();
//    }
//
//    private String getRequestIp(final HttpServletRequest request) {
//        String ip = request.getHeader("X-Real-IP");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("X-Forwarded-For");// 这是一个可以伪造的头
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getRemoteAddr();
//            }
//        }
//        // 最后一个为RemoteAddr
//        int pos = ip.lastIndexOf(',');
//        if (pos >= 0) {
//            ip = ip.substring(pos);
//        }
//        if ("0:0:0:0:0:0:0:1".equals(ip)) {
//            ip = "127.0.0.1";
//        }
//        return ip;
//    }
//}
//